package com.bjjnts.tread;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import com.bjjnts.service.WatchService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
public class RunClientThread extends Thread {

    private List<JSONObject> taskList;

    private WatchService watchService;

    private boolean flag = true;

    private int sleepWatchTime = 60;

    int errorCount = 0;

    private RunClientThread() {}

    public RunClientThread(List<JSONObject> taskList, WatchService watchService) {
        this.taskList = taskList;
        this.watchService = watchService;
    }

    public void study() throws Exception {
        if (errorCount > 10) {
            log.info("课程进度错误大于最大..停止");
            flag = false;
            return ;
        }
        if (CollUtil.isEmpty(taskList)) {
            // 停止
            log.info("well done...");
            flag = false;
            return ;
        }
        JSONObject study = taskList.get(0);
//        log.info("old " + JSONUtil.toJsonStr(study));
        // 校验学习进度
        if (Objects.equals(study.getStr("time"), study.getStr("progress_time"))) {
            // 满足条件的移除
            log.info("已经看完：" + study.getStr("course_name"));
            taskList.remove(0);
            study();
        }else {
            int time = 60;
            Integer progress_time = study.getInt("progress_time");
            if (Objects.equals(progress_time, 0)) {
                time = 0;
            }
            if (Objects.equals(progress_time, 1)) {
                time = 59;
            }
            boolean is_end = false;
            // 要结束了
            if (study.getInt("time") - study.getInt("progress_time") < 60) {
                is_end = true;
                time = study.getInt("time") - study.getInt("progress_time");
            }
            int new_progress_time = progress_time + time;
            log.info("正在观看：" + study.getStr("course_name") + "," + "观看进度：" + study.getStr("progress_time") + ",总进度：" + study.getStr("time") + "。");
            JSONObject rep = watchService.studies(study.getStr("class_id"), study.getStr("course_id"), study.getStr("unit_id"), study.getStr("video_id"), new_progress_time, is_end);
            // 正常返回
            if (BeanUtil.isEmpty(rep) || Objects.isNull(rep.getStr("id"))) {
                // 重置errorCount
                errorCount = 0;
                if (Objects.equals(progress_time, 0)) {
                    study.set("progress_time", 1);
                }else {
                    study.set("progress_time", progress_time + time);
                }
                taskList.set(0, study);
            }
            // 学习进度错误
            if(Objects.equals(rep.getInt("code"), 3001)) {
                errorCount++;
                // 休息三分钟
                log.info("进度错误,错误次数：" + errorCount + ",莫慌...重置进度中...");
                int sleepTime = 60 * (errorCount + 2);
                TimeUnit.SECONDS.sleep(sleepTime);
                this.taskList = watchService.getTaskList();
            }
            // 学习总时长每天8小时
            if (Objects.equals(rep.getInt("code"), 3003)) {
                this.taskList = new ArrayList<>();
                log.info("打完收工。");
                this.flag = false;
            }
        }
    }

    @SneakyThrows
    @Override
    public void run() {
        while (flag) {
            study();
            TimeUnit.SECONDS.sleep(sleepWatchTime);
        }
    }
}
